package com.clean.cleantotal.compores.delete

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.FunType.EMPTY
import com.clean.cleantotal.bean.FunType.INS
import com.clean.cleantotal.bean.FunType.LARGE
import com.clean.cleantotal.bean.FunType.TEL
import com.clean.cleantotal.bean.FunType.TIK
import com.clean.cleantotal.bean.FunType.WHATSAPP
import com.clean.cleantotal.compores.result.ResultActivity
import com.clean.cleantotal.databinding.ActivityDeleteBinding
import com.clean.cleantotal.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

class DeleteActivity : BaseActivity<ActivityDeleteBinding>() {
    override val viewBinding: ActivityDeleteBinding
        get() = ActivityDeleteBinding.inflate(layoutInflater)

    private var funType: FunType? = null
    private var totalSize = 0L
    private var title = ""
    override fun initParams(extras: Bundle?) {
        super.initParams(extras)
        extras?.let {
            funType = it.getSerializable("funType") as FunType?
            title = it.getString("title", "")
        }
    }

    override fun initView() {
        try {
            animal = Utils.showAnimalProcess(mBinding.scanLayout.tvPer) {
                openActivity(ResultActivity::class.java, Bundle().apply {
                    putSerializable("funType", funType)
                    putLong("totalSize", totalSize)
                    putString("title", title)
                    finish()
                })
            }
            mBinding.scanLayout.animal.setAnimation("delete.json")
            mBinding.scanLayout.tvContent.text = "Cleaning Up..."
            when (funType) {
                LARGE, FunType.CLEAN -> {
                    totalSize = Utils.cleanJunFiles.sumOf { it.size }
                    lifecycleScope.launch(Dispatchers.IO) {
                        Utils.cleanJunFiles.forEach {
                            val file = File(it.path)
                            file.setWritable(true)
                            file.delete()
                        }
                        withContext(Dispatchers.Main) {
                            animal?.duration = 2000
                        }
                    }
                }

                EMPTY -> {
                    totalSize = 0L
                    lifecycleScope.launch(Dispatchers.IO) {
                        Utils.selectEmptyList.forEach {
                            val file = File(it.path)
                            file.setWritable(true)
                            file.delete()
                        }
                        withContext(Dispatchers.Main) {
                            animal?.duration = 2000
                        }
                    }
                }

                TIK, INS, TEL, WHATSAPP -> {
                    totalSize = Utils.selectTikList.sumOf { it.size }
                    lifecycleScope.launch(Dispatchers.IO) {
                        Utils.selectTikList.forEach {
                            val file = File(it.path)
                            file.setWritable(true)
                            file.delete()
                        }
                        withContext(Dispatchers.Main) {
                            animal?.duration = 2000
                        }
                    }
                }

                else -> {}
            }
            mBinding.scanLayout.animal.playAnimation()
        } catch (e: Exception) {
            e.printStackTrace()
            openActivity(ResultActivity::class.java, Bundle().apply {
                putSerializable("funType", funType)
                putLong("totalSize", totalSize)
                finish()
            })
        }
    }

    override fun onBackPressed() {

    }
}